package com.zx.jhpt.job;

/**
 * 同步风险到数据集市
 * Created by Administrator on 2016/3/3.
 */

import com.zx.framework.core.util.DatetimeUtils;
import com.zx.jhpt.excel.Engine;
import com.zx.jhpt.excel.vo.Risk;
import com.zx.jhpt.po.Fxfkjz;
import com.zx.jhpt.service.DynamicService;
import com.zx.jhpt.service.EngineService;
import com.zx.jhpt.service.FxfkjzService;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 定时拉取数据
 */
@Component
public class DynamicRiskQuartz {
    private static final Logger LOGGER = Logger.getLogger(DynamicRiskQuartz.class);

    @Resource
    private EngineService engineService;
    @Resource
    private DynamicService dynamicService;
    @Resource
    private FxfkjzService fxfkjzService;

    private static boolean flag = true;
    /**
     * 每天定时拉取数据,每月20号23点执行
     */
    @Scheduled(cron = "0 0 23 20 * ?")
    public void pullData() {
        if(flag){
            LOGGER.debug("DynamicRiskQuartz start.");
            flag = false;
            try{
                List<Fxfkjz> fxfkjzs = fxfkjzService.getAll();
                String cycle = DatetimeUtils.dateToString(new Date(),"yyyy-MM");
                for(Fxfkjz fxfkjz:fxfkjzs){
                    Risk r = Engine.getRisk(fxfkjz.getId());
                    List<Map<String,Object>> datas = engineService.loadDatas(r, cycle);
                    dynamicService.dataTransfer(r, datas, cycle);
                }
            }catch (Exception e){
                LOGGER.warn("DynamicRiskQuartz fail:"+e.getMessage(),e);
            }
            flag = true;
            LOGGER.debug("DynamicRiskQuartz start.");
        }
    }
}
